package com.example.gitzuoye3;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

/**
 * 电影列表适配器，用于在ListView中显示电影列表
 */
public class shipeiqi extends BaseAdapter {

    // 详情按钮点击监听器接口
    public interface OnDetailButtonClickListener {
        void onDetailButtonClick(xiangxi.DianyingItem item);
    }

    // 收藏状态变化监听器接口
    public interface OnFavoriteChangeListener {
        void onFavoriteChanged();
    }

    private Context context; // 上下文对象
    private List<xiangxi.DianyingItem> dianyingItems; // 电影数据列表
    private LayoutInflater inflater; // 布局加载器
    private SharedPreferences favoritesPrefs; // 收藏状态存储
    private OnDetailButtonClickListener detailListener; // 详情按钮监听器
    private OnFavoriteChangeListener favoriteChangeListener; // 收藏状态变化监听器
    private SQL sqlHelper; // 数据库帮助类

    /**
     * 构造函数
     * @param context 上下文
     * @param dianyingItems 电影数据列表
     * @param listener 详情按钮点击监听器
     */
    public shipeiqi(Context context, List<xiangxi.DianyingItem> dianyingItems,
                    OnDetailButtonClickListener listener) {
        this.context = context;
        this.dianyingItems = dianyingItems;
        this.inflater = LayoutInflater.from(context);
        this.favoritesPrefs = context.getSharedPreferences("favorites", Context.MODE_PRIVATE);
        this.detailListener = listener;
        this.sqlHelper = new SQL(context);
    }

    /**
     * 设置收藏状态变化监听器
     * @param listener 监听器
     */
    public void setFavoriteChangeListener(OnFavoriteChangeListener listener) {
        this.favoriteChangeListener = listener;
    }

    @Override
    public int getCount() {
        return dianyingItems.size();
    }

    @Override
    public xiangxi.DianyingItem getItem(int position) {
        return dianyingItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final xiangxi.DianyingItem item = getItem(position);

        // 1. 视图复用逻辑
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.xinxi, parent, false);
            holder = new ViewHolder();
            holder.dianyingImage = convertView.findViewById(R.id.dianying_image);
            holder.dianyingName = convertView.findViewById(R.id.dianying_name);
            holder.dianyingDescription = convertView.findViewById(R.id.dianying_description);
            holder.btnDetail = convertView.findViewById(R.id.btn_detail);
            holder.btnFavorite = convertView.findViewById(R.id.btn_favorite);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 2. 设置电影数据
        holder.dianyingName.setText(item.getName());
        holder.dianyingDescription.setText(item.getDescription());

        // 3. 加载电影图片
        int imageResId = context.getResources().getIdentifier(
                item.getImageName(),
                "drawable",
                context.getPackageName());

        if (imageResId != 0) {
            holder.dianyingImage.setImageResource(imageResId);
        } else {
            holder.dianyingImage.setImageResource(R.drawable.kongbai);
        }

        // 4. 设置详情按钮点击事件
        holder.btnDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detailListener != null) {
                    detailListener.onDetailButtonClick(item);
                }
            }
        });

        // 5. 设置收藏按钮状态和点击事件
        boolean isFavorite = favoritesPrefs.getBoolean(String.valueOf(item.getId()), false);
        holder.btnFavorite.setImageResource(isFavorite ?
                android.R.drawable.star_big_on : android.R.drawable.star_big_off);

        holder.btnFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean newFavorite = !favoritesPrefs.getBoolean(String.valueOf(item.getId()), false);
                favoritesPrefs.edit()
                        .putBoolean(String.valueOf(item.getId()), newFavorite)
                        .apply();

                Toast.makeText(context, newFavorite ? "已收藏" : "已取消收藏",
                        Toast.LENGTH_SHORT).show();

                // 通知收藏状态变化
                if (favoriteChangeListener != null) {
                    favoriteChangeListener.onFavoriteChanged();
                }

                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    /**
     * ViewHolder模式，用于优化列表性能
     */
    static class ViewHolder {
        ImageView dianyingImage;      // 电影图片
        TextView dianyingName;       // 电影名称
        TextView dianyingDescription; // 电影描述
        ImageButton btnDetail;      // 详情按钮
        ImageButton btnFavorite;   // 收藏按钮
    }
}